<?php

namespace app\api\controller;

use app\api\model\UserModel;
use app\api\utils\jsonResult;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Session;

class User extends Controller
{
    /**********用户注册API接口***********/
    public function userRegister(): \think\response\Json
    {
        $userModel = new UserModel();
        $username = input('param.user_name');
        $data = [
            'user_name' => input('param.user_name'),
            'password' => input('param.password'),
            'tel' => input('param.tel'),
            'nick_name' => input('param.nick_name'),
        ];
        if ($userModel->searchByName($username)) {
            return (new jsonResult)::result(201, "注册失败，该用户名已存在");
        } else
        {
            if ($userModel ->add($data))
            {
                return (new jsonResult)::result(200, "用户注册成功");
            }
            else return (new jsonResult)::result(500, "服务器错误");
        }
    }

    public function userLogin(): \think\response\Json
    {
        $user_name = input('param.user_name');
        $password = input('param.password');
        //$pwd = md5($pwd);
        $userModel = new UserModel();
        $admin = Db::table('user_info')->where(['user_name' => $user_name, 'password' => $password])->select();
        if (!$admin) {
            if (!$userModel->searchByName($user_name))
                return (new jsonResult)::result(201, "用户名错误或用户不存在");
            else
                return (new jsonResult)::result(202, "密码错误");
        } else {
            $user_id = $userModel->findIdByName($user_name);
            Session::set('user_name', $user_name);
            Session::set('user_id', $user_id);
            $userData = $userModel->getUserData($user_name);
            return (new jsonResult)::result(200, "登陆成功", $userData);
        }

    }

    /**********用户个人信息更新API接口***********/
    public function userUpdate(): \think\response\Json
    {
        $userModel = new UserModel();
        $username = input('param.user_name');
        $data = [
            'password' => input('param.password'),
            'tel' => input('param.tel'),
            'nick_name' => input('param.nick_name'),
        ];
        if ($userModel->updateByUsername($username, $data)) {
            return (new jsonResult)::result(200, "用户信息更新成功");
        } else return (new jsonResult)::result(500, "用户信息更新失败");
    }

    public function passwordChange(): \think\response\Json
    {
        $userModel = new UserModel();
        $username = input('param.user_name');
        $data = [
            'password' => input('param.password')
        ];
        if ($userModel->updatePassword($username, $data)) {
            return (new jsonResult)::result(200, "用户密码修改成功");
        } else return (new jsonResult)::result(500, "用户修改失败");
    }

    public function sessionCheck(): \think\response\Json
    {
        if (Session::get('user_name')) {
            return (new jsonResult)::result(200, "用户已登陆");
        } else {
            return (new jsonResult)::result(401, "用户未登陆或登陆过期");
        }
    }
}